define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init();

            //绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));
                if (panel.size() > 0) {
                    Controller.table[panel.attr("id")].call(this);
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }
                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });

            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
        },
        table: {
            dorm: function () {
                var table_dorm = $("#table_dorm");
                table_dorm.bootstrapTable({
                    url: 'record/dorm/index?id=0',
                    extend: {
                        "del_url": "record/dorm/del",
                    },
                    toolbar: '#toolbar_dorm',
                    commonSearch: true,
                    searchFormVisible: true,
                    sortName: 'id',
                    sortOrder: 'desc',
                    columns: [
                        [
                            {field: 'state', checkbox: true,},
                            {field: 'json_data.school_name', title: '学校', operate:false,},
                            {field: 'json_data.garden_name', title: '园区', operate:false,},
                            {field: 'json_data.building_name', title: '楼栋', operate:false,},
                            {field: 'json_data.dorm_number', title: '寝室编号', operate:false,},
                            {field: 'behavior_name', title: __('点评行为'), operate: false},
                            {
                                field: 'behavior_type_text',
                                title: __('类型'),
                                operate: false,
                                formatter: Controller.api.formatter.label
                            },
                            {field: 'score', title: __('点评分值'), operate: false},
                            {field: 'record_username', title: __('点评人'), operate: false},
                            {
                                field: 'created_at',
                                title: __('Create time'),
                                sortable: true,
                                formatter: Table.api.formatter.datetime,
                                operate: 'RANGE',
                                addclass: 'datetimerange'
                            },
                            {field: 'operate', title: __('Operate'), table: table_dorm, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        ]
                    ],
                    onCheck:function () {
                        var data = table_dorm.bootstrapTable('getSelections');
                        console.log(data)
                    }
                });
                Table.api.bindevent(table_dorm);

                $(document).on('click', '.btn-import-dorm', function () {
                    Backend.api.open('record/dorm/import?type=dorm', '导入');
                });
            },
            student: function () {
                var table_student = $("#table_student");
                table_student.bootstrapTable({
                    url: 'record/dorm/index?id=1',
                    extend: {
                        "del_url": "record/dorm/del",
                    },
                    toolbar: '#toolbar_student',
                    commonSearch: true,
                    searchFormVisible: true,
                    sortName: 'id',
                    sortOrder: 'desc',
                    columns: [
                        [
                            {field: 'state', checkbox: true,},
                            {field: 'json_data.school_name', title: '学校', operate:false,},
                            {field: 'json_data.garden_name', title: '园区', operate:false,},
                            {field: 'json_data.building_name', title: '楼栋', operate:false,},
                            {field: 'json_data.dorm_number', title: '寝室编号', operate:false,},
                            {field: 'bed_num', title: '床位号', operate:false,},
                            {field: 'behavior_name', title: __('点评行为'), operate: false},
                            {
                                field: 'behavior_type_text',
                                title: __('类型'),
                                operate: false,
                                formatter: Controller.api.formatter.label
                            },
                            {field: 'score', title: __('点评分值'), operate: false},
                            {field: 'record_username', title: __('点评人'), operate: false},
                            {
                                field: 'created_at',
                                title: __('Create time'),
                                sortable: true,
                                formatter: Table.api.formatter.datetime,
                                operate: 'RANGE',
                                addclass: 'datetimerange'
                            },
                            {field: 'operate', title: __('Operate'), table: table_student, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        ]
                    ]
                });
                Table.api.bindevent(table_student);
                $(document).on('click', '.btn-import-student', function () {
                    Backend.api.open('record/dorm/import?type=student', '导入');
                });
            }
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        import: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                label: function (value, row, index) {
                    if (row.behavior_type == '1') {
                        return '<span class="label label-success">' + value + '</span>';
                    } else {
                        return '<span class="label label-danger">' + value + '</span>';
                    }
                }
            },
        }
    };
    return Controller;
});

